package com.bestcem.xm.ticket.dao.converter;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.ticket.entity.mongo.Record;
import com.bestcem.xm.ticket.entity.mongo.TicketRule;
import com.bestcem.xm.ticket.entity.mysql.RecordDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.annotation.Nullable;
import java.util.Objects;

/**
 * DO和DTO转换类
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/17 16:10
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RecordConverter {

    @Nullable
    public static Record doToDto(RecordDO recordDO) {
        if (Objects.isNull(recordDO)) {
            return null;
        }

        Record record = new Record();
        record.setId(recordDO.getRecordId());
        record.setOrgId(recordDO.getOrgId());
        record.setMode(recordDO.getMode());
        record.setQProjectId(recordDO.getQProjectId());
        record.setRspdSeq(recordDO.getRspdSeq());
        record.setAlertSeq(recordDO.getAlertSeq());
        record.setGroupList(CommonConverter.stringToList(recordDO.getGroupList(), String.class));
        record.setGroupCode(recordDO.getGroupCode());
        record.setRuleList(CommonConverter.stringToList(recordDO.getRuleList(), TicketRule.Condition.class));
        record.setCloseDate(recordDO.getCloseDate());
        record.setCloseReason(recordDO.getCloseReason());
        record.setData(CommonConverter.stringToObject(recordDO.getData(), JSONObject.class));

        CommonConverter.setEntity(record, recordDO);
        return record;
    }


    @Nullable
    public static RecordDO dtoToDo(Record record) {
        if (Objects.isNull(record)) {
            return null;
        }

        RecordDO recordDO = new RecordDO();
        recordDO.setRecordId(record.getId());
        recordDO.setOrgId(record.getOrgId());
        recordDO.setMode(record.getMode());
        recordDO.setQProjectId(record.getQProjectId());
        recordDO.setRspdSeq(record.getRspdSeq());
        recordDO.setAlertSeq(record.getAlertSeq());
        recordDO.setGroupList(CommonConverter.listToString(record.getGroupList()));
        recordDO.setGroupCode(record.getGroupCode());
        recordDO.setRuleList(CommonConverter.listToString(record.getRuleList()));
        recordDO.setCloseDate(record.getCloseDate());
        recordDO.setCloseReason(record.getCloseReason());
        recordDO.setData(CommonConverter.objectToString(record.getData()));
        recordDO.setSample(record.getSample());

        CommonConverter.setDo(record, recordDO);
        return recordDO;
    }
}